package org.j5108.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.j5108.dao.StudentsDAO;
import org.j5108.entity.Students;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:applicationContext.xml"})
public class UsersDao {

	@Autowired
    private SessionFactory sessionFactory;
    @Test
    @Transactional
    public void tsetfindAllObject() {
    	Session s = sessionFactory.getCurrentSession();
    	int id = 2 ;
    	s.beginTransaction();
    	Students c = s.get(Students.class,id);
    	System.out.println(c+")))))))))))))))))))))))))");
    	Query q = s.createQuery("from Students");
    	q.setFirstResult(2);
    	q.setMaxResults(2);
    	List<Students> cats = (ArrayList<Students>)q.list();
    	System.out.println(cats);
    	try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
    	try {
			Connection conn =  DriverManager.getConnection("jdbc:mysql://localhost:3306/test");
			Statement st = conn.createStatement();
//			ResultSet rs = st.execute("sql");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//    	int result = 0;
//        try {
//            s.delete(c);
//            s.flush();
//            System.out.println(c+"----------------------------------------");
//            result = 1;
//        } catch ( Exception e ) {
//            result = 0;
//            e.printStackTrace();
//        }
//        System.out.println(result+"----------------");
//    	s.getTransaction().commit();	
//    	System.out.println(new StudentsDAO().findAll());
//        List<Users> usersList = s.selectList("org.news.beans.selectAll");
//        for ( Users u : usersList ) {
//            System.out.println(u + "OOOOOOOOOOOOOOOOOO");
//        }
    }
}